package com.wsm.blog.controller;

import com.wsm.blog.domain.ContentDO;
import com.wsm.blog.service.ContentService;
import com.wsm.common.annotation.DuplicateSubmitToken;
import com.wsm.common.annotation.OperateLog;
import com.wsm.common.controller.BaseController;
import com.wsm.common.utils.*;
import com.wsm.system.domain.UserDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章内容
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-09 10:03:34
 */
@Api(tags = "博客编辑")
@Controller
@RequestMapping("/blog/bContent")
public class ContentController extends BaseController {
	@Autowired
    ContentService bContentService;

	@GetMapping()
	@ApiOperation("跳转博客主页")
	@RequiresPermissions("blog:bContent:bContent")
	String bContent(Model model) {
		return "blog/bContent/bContent";
	}

	@ResponseBody
	@GetMapping("/list")
	@ApiOperation("博客列表")
	@RequiresPermissions("blog:bContent:bContent")
	public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
		params.put("created", ShiroUtils.getUser().getUserId());
		params.put("delFlag", 1);
		Query query = new Query(params);
		List<ContentDO> bContentList = bContentService.listNotHaveContent(query);
		int total = bContentService.count(query);
		PageUtils pageUtils = new PageUtils(bContentList, total);
		return Result.success(pageUtils);
	}

	@GetMapping("/add" )
	@ApiOperation("跳转添加博客界面")
	@RequiresPermissions("blog:bContent:add" )
	String add(Model model) {
		UserDO user = ShiroUtils.getUser();
		model.addAttribute("user", user);
		return "blog/bContent/add";
	}

	@OperateLog("编辑博客")
	@ApiOperation("跳转编辑博客界面")
	@GetMapping("/edit/{cid}")
	@RequiresPermissions("blog:bContent:edit")
	String edit(@PathVariable("cid" ) Long cid, Model model) {
		ContentDO bContentDO = bContentService.get(cid);
		model.addAttribute("bContent", bContentDO);
		return "blog/bContent/edit";
	}

	/**
	 * 保存
	 */
	@DuplicateSubmitToken
	@OperateLog("保存博客")
	@ResponseBody
	@ApiOperation("保存博客")
	@RequiresPermissions("blog:bContent:add")
	@PostMapping("/save")
	public Result save(ContentDO bContent) {
		if (bContent.getAllowComment() == null) {
			bContent.setAllowComment(0);
		}
		if (bContent.getAllowPing() == null) {
			bContent.setAllowPing(0);
		}
		if(StringUtils.isEmpty(bContent.getAliasName())){
			bContent.setAliasName(UUIDUtil.generateShortUuid());
		}
		bContent.setGtmCreate(new Date());
		bContent.setCreated(ShiroUtils.getUserId());
		boolean count;
		if (bContent.getCid() == null || "".equals(bContent.getCid())) {
			count = bContentService.save(bContent);
		} else {
			count = bContentService.update(bContent);
		}
		if (count) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 修改
	 */
	@DuplicateSubmitToken
	@OperateLog("编辑博客")
	@ApiOperation("编辑博客")
	@RequiresPermissions("blog:bContent:edit")
	@ResponseBody
	@RequestMapping("/update")
	public Result update(ContentDO bContent) {
		bContent.setGtmCreate(new Date());
		bContentService.update(bContent);
		return Result.success();
	}

	/**
	 * 删除
	 */
	@DuplicateSubmitToken
	@OperateLog("删除博客")
	@ApiOperation("删除博客")
	@RequiresPermissions("blog:bContent:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(Long id) {
		if (bContentService.remove(id) > 0) {
			return Result.success();
		}
		return Result.error();
	}

	/**
	 * 删除
	 */
	@DuplicateSubmitToken
	@OperateLog("批量删除博客")
	@ApiOperation("批量删除博客")
	@RequiresPermissions("blog:bContent:batchRemove")
	@PostMapping("/batchRemove")
	@ResponseBody
	public Result remove(@RequestParam("ids[]" ) Long[] cids) {
		bContentService.batchRemove(cids);
		return Result.success();
	}
}
